package com.bestcem.xm.survey.export.type;


import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.survey.export.BaseTypeHandler;
import com.bestcem.xm.survey.grpc.client.dto.QstructsDto;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

import static com.bestcem.xm.survey.enums.QuestionTypeEnum.QUESTION_TYPE_CITY;
import static com.bestcem.xm.survey.util.SurveyConstant.LEVEL_TITLE;

@Component
public class CityHandler implements BaseTypeHandler {

    @Override
    public Map<String, String> getOpenColumnChangeDictV2(JSONObject data) {
        Map<String, String> res = new HashMap();
        Integer info_level = (Integer) data.getOrDefault("info_level", 4);
        for (int i = 0; i < info_level; i++) {
            res.put(getOpenRealcolV2(data, i), getOpenReadablecolV2(data, i));
        }
        return res;
    }

    private String getOpenRealcolV2(JSONObject data, Integer info_level) {
        if (Objects.nonNull(info_level)) {
            String cid = data.getString("cid");
            return Arrays.asList(cid + "__province", cid + "__city", cid + "__district", cid + "__street").get(info_level);
        }
        return null;
    }


    private String getOpenReadablecolV2(JSONObject data, Integer info_level) {
        if (Objects.nonNull(info_level)) {
            String title = BaseTypeHandler.clearHtml(data.getString("title"));
            return Arrays.asList(title + "__省份", title + "__城市", title + "__区县", title + "__街道信息").get(info_level);
        }
        return null;
    }

    @Override
    public List<String> getOpenColumnListV2(JSONObject data) {
        List<String> res = new ArrayList();
        Integer info_level = (Integer) data.getOrDefault("info_level", 4);
        for (int i = 0; i < info_level; i++) {
            res.add(getOpenRealcolV2(data, i));
        }
        return res;
    }

    @Override
    public Map<Object, Object> getOpenAnswerDictV2(JSONObject dataValue, Object answerValue) {
        Map<Object, Object> data = new HashMap<>();
        if (Objects.isNull(answerValue)) {
            Integer info_level = (Integer) dataValue.getOrDefault("info_level", 4);
            for (java.lang.Integer level = 0; level < info_level; level++) {
                data.put(getOpenRealcolV2(dataValue, level), null);
            }
        } else {
            JSONObject answers = (JSONObject) answerValue;
            answers.forEach((index, answer) -> data.put(getOpenRealcolV2(dataValue, Integer.valueOf(index) - 1), answer));
        }
        return data;
    }

    @Override
    public List<List<String>> getVarLabelsV2(JSONObject dataValue, boolean not01, boolean need_title) {
        List<List<String>> data = new ArrayList<>();
        List<String> var_list = new ArrayList(), val_list = new ArrayList();
        Map<String, String> cityValue = getCityLevelInfoDict(dataValue);
        getOpenColumnListV2(dataValue).forEach(open_col ->
                var_list.add(String.format("VARIABLE LABELS %s '%s'.", open_col, getUnquotedStr(cityValue.get(open_col), true))));
        data.add(var_list);
        data.add(val_list);
        return data;
    }

    private Map<String, String> getCityLevelInfoDict(JSONObject data) {
        Map<String, String> res = new HashMap();
        Integer info_level = (Integer) data.getOrDefault("info_level", 4);
        for (int level = 0; level < info_level; level++) {
            res.put(getOpenRealcolV2(data, level), getLevelTitleInfo(level));
        }
        return res;
    }

    private String getLevelTitleInfo(Integer info_level) {
        if (Objects.nonNull(info_level)) {
            return LEVEL_TITLE.get(info_level);
        }
        return null;
    }

    @Override
    public List<String> getSpssCodeV2(JSONObject param, QstructsDto qstruct) {
        return getCityLevelInfoDict(param).keySet().stream().collect(Collectors.toList());
    }

    @Override
    public String getQuestionTypeValue() {
        return QUESTION_TYPE_CITY.getValue();
    }
}
